package IOtest;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class DidryTest {
    private static final String DIARY_DIR = "D:/diary";

    // 显示功能菜单
    private static void showMenu() {
        System.out.println("1. 新建日记本");
        System.out.println("2. 写日记");
        System.out.println("3. 修改日记");
        System.out.println("4. 查看日记");
        System.out.println("5. 退出程序");

    }
    // 新建日记本
    private static void newDiary() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入日记本名称：");
        String diaryName = scanner.nextLine();
        String filePath = DIARY_DIR + "/" + diaryName + ".txt";
        File file = new File(filePath);
        File dir = new File(DIARY_DIR);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        try {
            if (file.createNewFile()) {
                System.out.println("已创建名为 " + diaryName + " 的日记本");
            } else {
                System.out.println("名为 " + diaryName + " 的日记本已存在");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    // 写日记
    private static void writeDiary() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入要写日记的日记本名称：");
        String diaryName = scanner.nextLine();
        String filePath = DIARY_DIR + "/" + diaryName + ".txt";
        File file = new File(filePath);
        if (!file.exists()) {
            System.out.print("名为 " + diaryName + " 的日记本不存在，是否创建？(y/n) ");
            String choice = scanner.nextLine();
            if ("y".equalsIgnoreCase(choice)) {
                try {
                    file.createNewFile();
                    System.out.print("请输入日记内容：");
                    String content = scanner.nextLine();
                    writeToFile(filePath, content);
                    System.out.println("日记已写入");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else {
                System.out.println("操作取消");
            }
        } else {
            long fileSize = file.length();
            if (fileSize > 0) {
                System.out.println("日记本已有内容，选择操作：1.覆盖 2.追加 3.取消 ");
                String operation = scanner.nextLine();
                if ("1".equals(operation)) {
                    System.out.print("请输入新的日记内容：");
                    String newContent = scanner.nextLine();
                    writeToFile(filePath, newContent);
                    System.out.println("日记已覆盖");
                } else if ("2".equals(operation)) {
                    System.out.print("请输入要追加的日记内容：");
                    String appendContent = scanner.nextLine();
                    appendToFile(filePath, appendContent);
                    System.out.println("日记已追加");
                } else {
                    System.out.println("操作取消");
                }
            } else {
                System.out.print("请输入日记内容：");
                String content = scanner.nextLine();
                writeToFile(filePath, content);
                System.out.println("日记已写入");
            }
        }
    }
    // 查看日记
    private static void viewDiary() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入要查看的日记本名称：");
        String diaryName = scanner.nextLine();
        String filePath = DIARY_DIR + "/" + diaryName + ".txt";
        File file = new File(filePath);
        if (!file.exists()) {
            System.out.println("名为 " + diaryName + " 的日记本不存在");
        } else {
            try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
                String line;
                while ((line = reader.readLine()) != null) {
                    System.out.println(line);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    //修改日记
    private static void modifyDiary() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入要修改的日记本名称：");
        String diaryName = scanner.nextLine();
        String filePath = DIARY_DIR + "/" + diaryName + ".txt";
        File file = new File(filePath);
        if (!file.exists()) {
            System.out.println("名为 " + diaryName + " 的日记本不存在");
        }
    }
    // 写入文件（覆盖方式）
    private static void writeToFile(String filePath, String content) {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
            writer.write(content);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 追加写入文件
    private static void appendToFile(String filePath, String content) {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath, true))) {
            writer.write(content);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            showMenu();
            System.out.print("请选择功能(1-5)：");
            String choice = scanner.nextLine();
            switch (choice) {
                case "1":
                    newDiary();
                    break;
                case "2":
                    writeDiary();
                    break;
                case "3":
                    modifyDiary();
                    break;
                case "4":
                    viewDiary();
                    break;
                case "5":
                    System.out.println("程序退出");
                    return;
                default:
                    System.out.println("无效的选择，请重新输入");
            }
        }
    }
}